package dao

import (
	"database/sql"
	dbMysql "iris-shop/common/db/mysql"
	"iris-shop/common/models"
	"strconv"
)

type IProduct interface {
	Conn() (error)
	Insert(*models.Product) (int64, error)
	Delete(int64) bool
	Update(*models.Product) error
	SelectByKey(int64) (*models.Product, error)
	SelectAll()([]*models.Product, error)
	SubNumberOne(int64) error
}


type ProductManager struct {
	table string
	mysqlConn *sql.DB
}

func (p *ProductManager) Conn() error {
	if p.mysqlConn == nil {
		mysql := dbMysql.DBConn()
		p.mysqlConn = mysql
	}

	if p.table == "" {
		p.table = "product"
	}
	return nil
}

func (p *ProductManager) Insert(product *models.Product) (productId int64, err error) {
	if err = p.Conn(); err != nil {
		return
	}

	sql := "INSERT product SET productName=?, productNum=?,productImage=?,productUrl=?"
	stmt, err := p.mysqlConn.Prepare(sql)
	defer stmt.Close()
	if err != nil {
		return
	}

	result, err := stmt.Exec(product.ProductName,product.ProductNum,product.ProductImage,product.ProductUrl)
	if err != nil {
		return
	}
	return result.LastInsertId()
}

func (p *ProductManager) Delete(productID int64) bool {
	if err:=p.Conn();err != nil{
		return false
	}
	sql := "delete from product where ID=?"
	stmt,err := p.mysqlConn.Prepare(sql)
	defer stmt.Close()
	if err!= nil {
		return false
	}
	_,err = stmt.Exec(strconv.FormatInt(productID,10))
	if err !=nil {
		return false
	}
	return true
}

func (p *ProductManager) Update(product *models.Product) error {
	//1.判断连接是否存在
	if err:=p.Conn();err != nil{
		return err
	}

	sql := "Update product set productName=?,productNum=?,productImage=?,productUrl=? where ID="+strconv.FormatInt(product.ID,10)

	stmt,err := p.mysqlConn.Prepare(sql)
	defer stmt.Close()
	if err !=nil {
		return err
	}

	_,err = stmt.Exec(product.ProductName,product.ProductNum,product.ProductImage,product.ProductUrl)
	if err !=nil {
		return err
	}
	return nil
}

func (p *ProductManager) SelectByKey(productID int64) (productResult *models.Product, err error) {
	if err=p.Conn();err != nil{
		return &models.Product{},err
	}
	sql := "Select * from "+p.table+" where ID ="+strconv.FormatInt(productID,10)
	row,errRow :=p.mysqlConn.Query(sql)
	defer row.Close()
	if errRow !=nil {
		return &models.Product{},errRow
	}
	result := dbMysql.GetResultRow(row)
	if len(result)==0{
		return &models.Product{},nil
	}
	productResult = &models.Product{}
	dbMysql.DataToStructByTagSql(result,productResult)
	return
}

func (p *ProductManager) SelectAll() (productArray []*models.Product, errProduct error) {
	if err:=p.Conn();err!= nil{
		return nil,err
	}
	sql := "Select * from "+p.table
	rows,err := p.mysqlConn.Query(sql)
	defer  rows.Close()
	if err !=nil {
		return nil ,err
	}

	result:= dbMysql.GetResultRows(rows)
	if len(result)==0{
		return nil,nil
	}

	for _,v :=range result{
		product := &models.Product{}
		dbMysql.DataToStructByTagSql(v,product)
		productArray=append(productArray, product)
	}
	return
}

func (p *ProductManager) SubNumberOne(productID int64) error {
	if err := p.Conn(); err != nil {
		return err
	}
	sql := "update "+p.table+" set "+" productNum=productNum-1 where ID="+strconv.FormatInt(productID, 10)
	stmt, err := p.mysqlConn.Prepare(sql)
	if err != nil {
		return err
	}
	_, err = stmt.Exec()
	return err
}

func NewProductManager(table string, db *sql.DB) IProduct {
	return &ProductManager{table: table,mysqlConn: db}
}